package org.gnyxy.service.impl;

import cn.bugstack.chatglm.model.ChatCompletionRequest;
import cn.bugstack.chatglm.model.ChatCompletionSyncResponse;
import cn.bugstack.chatglm.model.Model;
import cn.bugstack.chatglm.model.Role;
import cn.bugstack.chatglm.session.OpenAiSession;
import com.alibaba.fastjson.JSON;
import org.gnyxy.service.ChatglmService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

@Service
public class ChatglmServiceImpl implements ChatglmService {
    @Resource
    private OpenAiSession openAiSession;

    public String ExcelToJson(String goal, String value, String chartType) throws Exception {
        // 入参；模型、请求信息
        ChatCompletionRequest request = new ChatCompletionRequest();
        request.setModel(Model.CHATGLM_LITE_32K); // chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro
        request.setPrompt(new ArrayList<ChatCompletionRequest.Prompt>() {
            private static final long serialVersionUID = -7988151926241837899L;
            {
                add(ChatCompletionRequest.Prompt.builder()
                        .role(Role.user.getCode())
                        .content("分析需求：" + goal +
                                "原始数据：\n" +  value + "\n" +
                                "图表类型：" + chartType +
                                "请仅为Echarts创建一个图表配置对象的JSON文本内容，不要包含任何开头、结尾、注释。" +
                                "仅需要提供’option = {}'中的JSON文本内容。以下是格式仅供参考：\n" +
                                "{\n" +
                                        "  \"title\": {\n" +
                                        "    \"text\": \"调用成功率\"\n" +
                                        "  },\n" +
                                        "  \"tooltip\": {\n" +
                                        "    \"trigger\": \"axis\",\n" +
                                        "    \"axisPointer\": {\n" +
                                        "      \"type\": \"shadow\"\n" +
                                        "    }\n" +
                                        "  },\n" +
                                        "  \"legend\": {\n" +
                                        "    \"data\": [\"调用成功量\", \"调用失败量\"]\n" +
                                        "  },\n" +
                                        "  \"grid\": {\n" +
                                        "    \"left\": \"3%\",\n" +
                                        "    \"right\": \"4%\",\n" +
                                        "    \"bottom\": \"3%\",\n" +
                                        "    \"containLabel\": true\n" +
                                        "  },\n" +
                                        "  \"xAxis\": {\n" +
                                        "    \"type\": \"category\",\n" +
                                        "    \"boundaryGap\": false,\n" +
                                        "    \"data\": [\"2023-11-30\", \"2023-12-01\", \"2023-12-02\", \"2023-12-03\", \"2023-12-04\", \"2023-12-05\", \"2023-12-06\", \"2023-12-07\", \"2023-12-08\", \"2023-12-09\", \"2023-12-10\", \"2023-12-11\", \"2023-12-12\", \"2023-12-13\", \"2023-12-14\", \"2023-12-15\", \"2023-12-16\", \"2023-12-17\", \"2023-12-18\", \"2023-12-19\", \"2023-12-20\", \"2023-12-21\", \"2023-12-22\", \"2023-12-23\", \"2023-12-24\", \"2023-12-25\", \"2023-12-26\", \"2023-12-27\", \"2023-12-28\", \"2023-12-29\", \"2023-12-30\"]\n" +
                                        "  },\n" +
                                        "  \"yAxis\": {\n" +
                                        "    \"type\": \"value\"\n" +
                                        "  },\n" +
                                        "  \"series\": [\n" +
                                        "    {\n" +
                                        "      \"name\": \"调用成功量\",\n" +
                                        "      \"type\": \"bar\",\n" +
                                        "      \"stack\": \"总量\",\n" +
                                        "      \"data\": [0, 2449, 0, 16206, 0, 16814, 0, 0, 0, 0, 0, 0, 13439, 5814, 0, 12907, 22556, 43955, 0, 1064, 0, 0, 0, 0, 0]\n" +
                                        "    },\n" +
                                        "    {\n" +
                                        "      \"name\": \"调用失败量\",\n" +
                                        "      \"type\": \"bar\",\n" +
                                        "      \"stack\": \"总量\",\n" +
                                        "      \"data\": [0, 2449, 0, 16206, 0, 16814, 0, 0, 0, 0, 0, 0, 13439, 5814, 0, 12907, 22556, 43955, 0, 1064, 0, 0, 0, 0, 0]\n" +
                                        "    }\n" +
                                        "  ]\n" +
                                        "}")
                        .build());
            }
        });

        ChatCompletionSyncResponse response = openAiSession.completionsSync(request);

        return response.getData().getChoices().get(0).getContent();
    }

    public String ExcelToResult(String goal, String value) throws Exception {
        // 入参；模型、请求信息
        ChatCompletionRequest request = new ChatCompletionRequest();
        request.setModel(Model.CHATGLM_LITE); // chatGLM_6b_SSE、chatglm_lite、chatglm_lite_32k、chatglm_std、chatglm_pro
        request.setPrompt(new ArrayList<ChatCompletionRequest.Prompt>() {
            private static final long serialVersionUID = -7988151926241837899L;
            {
                add(ChatCompletionRequest.Prompt.builder()
                        .role(Role.user.getCode())
                        .content("分析需求：" + goal +
                                "原始数据：\n" +  value + "\n" +
                                "请您根据提供的原始数据并结合分析需求，直接撰写一份简洁明了的数据分析报告。要求字数150字内，言简意赅。" +
                                "请确保报告中只包含分析结果，不要包含任何前言、结尾或注释等附加内容。\n")
                        .build());
            }
        });

        ChatCompletionSyncResponse response = openAiSession.completionsSync(request);
        return response.getData().getChoices().get(0).getContent();
    }

}
